//
//  xsConfigurationMusicView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/29.
//  配音
// @[@"我的",@"官方"]

#import "xsConfigurationMusicView.h"
  
#define xrandomColor  [UIColor colorWithRed:(arc4random() %255)/255.0f green:(arc4random() %255)/255.0f blue:(arc4random() %255)/255.0f alpha:1.0f]

@interface xsConfigurationMusicView()<UIScrollViewDelegate,xsConfigurationMusicCollectionViewProtoCol>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UIButton *xsXbutton;// x按钮


@property (nonatomic, strong) UIScrollView *buttonsBgScrollView;
@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, strong) UIView *buttonsBgView;

@property (nonatomic,strong)UIScrollView *myMusicAuthorityBgScrollView;
@property (nonatomic,strong)xsConfigurationMusicCollectionView *myMusicCollectionView;
@property (nonatomic,strong)xsConfigurationMusicCollectionView *authorityCollectionView;// 官方权威

@property (nonatomic,strong) UIView *xsColorContourLineView;


@end

@implementation xsConfigurationMusicView

- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles{
    
    if (self = [super init]) {
        self.backgroundColor = COLOR_HEX(0x212333);
        
        [self addSubview:self.topView];
        
        [self.topView addSubview:self.searchButton];
        [self.topView addSubview:self.topTitleLabel];
        [self.topView addSubview:self.xsXbutton];
        
        [self addSubview:self.buttonsBgScrollView];
            
        [self.buttonsBgScrollView addSubview:self.buttonsBgView];
        [self.buttonsBgView addSubview:self.xsColorContourLineView];
        
        [self addSubview:self.myMusicAuthorityBgScrollView];
        
        [self.myMusicAuthorityBgScrollView addSubview:self.myMusicCollectionView];
        [self.myMusicAuthorityBgScrollView addSubview:self.authorityCollectionView];
            // 创建按钮数组
        self.buttons = buttonTitles;//@[@"Tab1", @"Tab2", @"Tab3"];
        
        self.layer.maskedCorners = UIRectCornerTopLeft|UIRectCornerTopRight;
        self.layer.cornerRadius = 10;
    }
    return self;
}

- (UIView *)buttonsBgView{
    if(!_buttonsBgView){
        _buttonsBgView = [[UIView alloc] init];
        _buttonsBgView.backgroundColor = UIColor.clearColor;
    }
    return _buttonsBgView;
}

- (UIScrollView *)buttonsBgScrollView
{
    if(!_buttonsBgScrollView){
        _buttonsBgScrollView = [[UIScrollView alloc] init];
        _buttonsBgScrollView.backgroundColor = UIColor.clearColor;
        
        _buttonsBgScrollView.delegate = self;
        _buttonsBgScrollView.pagingEnabled = YES;
        _buttonsBgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _buttonsBgScrollView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    
}


- (void)xsChangeFrame{
    
    CGFloat margin10 = 10;
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(margin10);
        make.top.equalTo(self.mas_top);//.offset(10);
        make.right.equalTo(self.mas_right).offset(-margin10);
        make.height.mas_equalTo(50);
    }];
    
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView.mas_left);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.xsXbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.topView.mas_right);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
    }];
    
    self.buttonsBgScrollView.frame = CGRectMake(0, 50+margin10, self.bounds.size.width, 40);
    
    CGFloat myMusicAuthorityBgScrollViewY = CGRectGetMaxY(self.buttonsBgScrollView.frame)+margin10;
    CGFloat myMusicAuthorityBgScrollViewX = margin10;
    CGFloat myMusicAuthorityBgScrollViewW = self.width-myMusicAuthorityBgScrollViewX*2.0;
    CGFloat myMusicAuthorityBgScrollViewH = self.height - myMusicAuthorityBgScrollViewY-10;
    self.myMusicAuthorityBgScrollView.frame = CGRectMake(myMusicAuthorityBgScrollViewX, myMusicAuthorityBgScrollViewY, myMusicAuthorityBgScrollViewW, myMusicAuthorityBgScrollViewH);
    self.myMusicAuthorityBgScrollView.backgroundColor = UIColor.clearColor;
    
    self.myMusicCollectionView.frame = CGRectMake(0, 0, self.myMusicAuthorityBgScrollView.width, self.myMusicAuthorityBgScrollView.height);
    self.authorityCollectionView.frame = CGRectMake(self.myMusicAuthorityBgScrollView.width, 0, self.myMusicAuthorityBgScrollView.width, self.myMusicAuthorityBgScrollView.height);
    
    self.myMusicAuthorityBgScrollView.contentSize = CGSizeMake(self.myMusicAuthorityBgScrollView.width*2, 0);
    
    UIFont *btnFont = Font(16);
    UIButton *lastButton;
    // 添加按钮到滚动视图
    for (int i = 0; i < self.buttons.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *buttonTitleStr = self.buttons[i];
        
        CGFloat buttonW = [buttonTitleStr calculateWidth:btnFont withHeight:40]+30.0;
        
        [button setBackgroundColor:UIColor.clearColor];
        [button setTitle:self.buttons[i] forState:UIControlStateNormal];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        button.titleLabel.font = btnFont;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [self.buttonsBgView addSubview:button];
        
        if (lastButton == nil) {
            button.titleLabel.font = FontBoldMT(16);
            button.frame = CGRectMake(10, 0, buttonW, 40);
            CGFloat lineX = button.x + (buttonW - 30)*0.5;
            CGFloat lineY = CGRectGetMaxY(button.frame)-3.0;
            self.xsColorContourLineView.frame = CGRectMake(lineX, lineY, 30, 3);
        }else{
            button.frame = CGRectMake(CGRectGetMaxX(lastButton.frame), 0, buttonW, 40);
        }
        lastButton = button;
    }

    // 设置内容视图的尺寸
    self.buttonsBgView.frame = CGRectMake(0, 0, CGRectGetMaxX(lastButton.frame), 40);
    self.buttonsBgScrollView.contentSize = self.buttonsBgView.frame.size;
}

- (void)buttonClicked:(UIButton *)sender {
    NSInteger selectedIndex = sender.tag;
    NSLog(@"Selected index: %ld", (long)selectedIndex);
    
    for (UIButton *tempButton in self.buttonsBgView.subviews) {
        if([tempButton isKindOfClass:[UIButton class]]){
            if(tempButton.tag!= sender.tag){
                tempButton.titleLabel.font = Font(16);
            }else{
                tempButton.titleLabel.font = FontBoldMT(16);
            }
        }
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.xsColorContourLineView.centerX = sender.centerX;
        [self.myMusicAuthorityBgScrollView setContentOffset:CGPointMake(self.myMusicAuthorityBgScrollView.width*selectedIndex, 0)];
    }];
    
}

- (void)updateConstraints{
    [super updateConstraints];
//    
//    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
}


- (xsConfigurationMusicCollectionView *)myMusicCollectionView{
    if(!_myMusicCollectionView){
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
//        NSArray *tempArr = @[@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓",@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓"];
//        
//        NSMutableArray *tempMarr = [NSMutableArray new];
//        for (int i =0; i<30; i++) {
//            xsConfigurationMusicCollectionViewCellModel *md = [xsConfigurationMusicCollectionViewCellModel new];
//            
//            int tpIndex = arc4random()%(tempArr.count);
//            md.titleString = [NSString stringWithFormat:@"%@",tempArr[tpIndex]];
//            md.headImg = @"bossIPDefaultImage";
//            [tempMarr addObject:md];
//        }
//        
        ////audioType;// 0我的 1官方
        _myMusicCollectionView = [[xsConfigurationMusicCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withAudioType:0];
        _myMusicCollectionView.backgroundColor = UIColor.clearColor;
        _myMusicCollectionView.delegateConfigurationMusic = self;
    }
    return _myMusicCollectionView;
}


- (xsConfigurationMusicCollectionView *)authorityCollectionView{
    if(!_authorityCollectionView){
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        ////audioType;// 0我的 1官方
        _authorityCollectionView = [[xsConfigurationMusicCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withAudioType:1];
        _authorityCollectionView.backgroundColor = UIColor.clearColor;
        _authorityCollectionView.delegateConfigurationMusic = self;
    }
    return _authorityCollectionView;
}

- (UIView *)topView{
    if(!_topView){
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = UIColor.clearColor;
    }
    return _topView;
}

- (UIButton *)xsXbutton{
    if (!_xsXbutton) {
        _xsXbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsXbutton.backgroundColor = UIColor.clearColor;
        [_xsXbutton addTarget:self action:@selector(xsXbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsXbutton setImage:[UIImage imageNamed:@"xsBossgou"] forState:UIControlStateNormal];
    }
    return _xsXbutton;
}

- (void)xsXbuttonAction:(UIButton *)btn{
    NSLog(@"xsXbuttonAction\n\n");
    if([self.delegate respondsToSelector:@selector(xsConfigurationMusicViewxsXbuttonBeClick:)]){
        [self.delegate xsConfigurationMusicViewxsXbuttonBeClick:btn];
    }
}

- (UIButton *)searchButton{
    if (!_searchButton) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchButton.backgroundColor = UIColor.purpleColor;
        [_searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_searchButton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
        _searchButton.hidden = YES;
    }
    return _searchButton;
}
- (void)searchButtonAction:(UIButton *)btn{
    NSLog(@"searchButtonAction\n\n");
   
    
}

- (UILabel *)topTitleLabel {
    if (!_topTitleLabel) {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.backgroundColor = UIColor.clearColor;
        _topTitleLabel.font = [UIFont systemFontOfSize:18 weight:(UIFontWeightBold)];// FontSemibold(17);
        _topTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _topTitleLabel.numberOfLines = 1;
        _topTitleLabel.text = @"选择配音";
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLabel;
}

- (UIScrollView *)myMusicAuthorityBgScrollView
{
    if(!_myMusicAuthorityBgScrollView){
        _myMusicAuthorityBgScrollView = [[UIScrollView alloc] init];
        _myMusicAuthorityBgScrollView.backgroundColor = UIColor.clearColor;
        _myMusicAuthorityBgScrollView.showsVerticalScrollIndicator = NO;
        _myMusicAuthorityBgScrollView.showsHorizontalScrollIndicator = NO;
        _myMusicAuthorityBgScrollView.pagingEnabled = YES;
        _myMusicAuthorityBgScrollView.delegate = self;
    }
    return _myMusicAuthorityBgScrollView;
}
- (UIView *)xsColorContourLineView{
    if(!_xsColorContourLineView){
        _xsColorContourLineView = [[UIView alloc] init];
        _xsColorContourLineView.backgroundColor = COLOR_White;
        
        _xsColorContourLineView.layer.cornerRadius = 1.5f;
        _xsColorContourLineView.layer.masksToBounds = YES;
    }
    return _xsColorContourLineView;
}


#pragma mark  ScollViewDelegate
// 滚动视图减速完成，滚动将停止时，调用该方法。一次有效滑动，只执行一次。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"scrollViewDidEndDecelerating");
    if(scrollView==self.myMusicAuthorityBgScrollView){
        int pageIndex = scrollView.contentOffset.x/self.myMusicAuthorityBgScrollView.frame.size.width;
        
        NSLog(@"scrollViewDidEndDecelerating--pageIndex==%d\n",pageIndex);
        for (UIButton *tempButton in self.buttonsBgView.subviews) {
            if([tempButton isKindOfClass:[UIButton class]])
            {
                if(pageIndex==tempButton.tag){
                    [self buttonClicked:tempButton];
                    break;
                }
            }
        }
    }
}

#pragma mark xsConfigurationMusicCollectionViewProtoCol
// // audioType;// 0我的 1官方
- (void)xsConfigurationMusicCollectionCellBeClick:(xsConfigurationMusicCollectionViewCellModel *)model withAudioType:(NSInteger)audioType{
    
    if(self.xsConfigurationMusicCellCallbackBlock){
        self.xsConfigurationMusicCellCallbackBlock(model, audioType);
    }
}

////audioType;// 0我的 1官方
// 改变全部的cell为选中或者未选中状态 YES选中  NO未选中
- (void)xsConfigurationMusicVChangAllCellClickState:(BOOL)clickStatu withAudioType:(NSInteger)audioType{
    
    if(audioType==0){
        [self.myMusicCollectionView xsConfigurationMusicCollectionChangAllCellClickState:clickStatu];
    }
    else if(audioType==1){
        [self.authorityCollectionView xsConfigurationMusicCollectionChangAllCellClickState:clickStatu];
    }
}

@end
